var _REG = new regexVerify();

/**
 *  验证器
 */
function regexVerify() {
    /**
     * 验证规则
     * @type {Array}
     */
    var ruleList = [];
    /**
     * 场景列表
     * @type {Array}
     */
    var sceneList = [];
    /**
     * 当前场景
     * @type {Array}
     */
    var sceneSingle = [];
    /**
     * 是否有场景
     * @type {boolean}
     */
    this.IsScene = false;
    /**
     * 内置算法
     * @type {{}}
     */
    var arithmetic = {};
     /**
     * 添加场景列表
     * @param data
     */
    this.addScene = function (data) {
        for (var info in data)
            sceneList[info] = data[info];
    };
    /**
     * 获取场景列表
     * @returns {Array|{}}
     */
    this.getScene = function (flg) {
        if (flg) return sceneList[flg];
        return sceneList;
    };
    /**
     * 添加规则列表
     * @param data
     */
    this.addRuleList = function (data) {
        for (var info in data)
            ruleList[info] = data[info];
    };
    /**
     * 获取规则列表
     * @returns {Array|{}}
     */
    this.getRuleList = function (flg) {
        if (sceneList[flg]) {
            var result = [];
            var list = this.getRoleInfo([], false);
            for (var i in sceneList[flg]) {
                var content = sceneList[flg][i].split("|") ? sceneList[flg][i].split("|") : sceneList[flg][i];
                var sub = content[1] ? content[1] : content[0];
                result[sub] = list[sub];
            }
            return result;
        }
        return ruleList;
    };
    /**
     * 添加算法
     * @param data
     */
    this.addArithmetic = function (data) {
        for (var info in data)
            arithmetic[info] = data[info];
    };
    /**
     * 获取算法
     * @returns {{}}
     */
    this.getArithmetic = function () {
        return arithmetic
    };
    /**
     * 设置当前场景
     * @param data
     */
    this.scene = function (data) {
        sceneSingle = sceneList[data] ? sceneList[data] : false;
        if (sceneSingle != false) this.IsScene = true;
        return this;
    };
    /**
     * 获取所有规则
     * @param allrole
     * @param data
     */
    this.haSubscript = function (allrole, data) {
        var list = [];
        for (var role in allrole)
            if (data[role] != undefined) list[role] = allrole[role];
        return list;
    };
    /**
     * 获取验证规则列表
     * @returns {Array}
     */
    this.getCheckList = function () {
        var list = [];
        var type = false;
        if (sceneSingle || sceneSingle.length > 0)
        //  场景所包含的规则
            for (var sc in sceneSingle) {
                type = 1;
                var alias = sceneSingle[sc].split("|");
                //  是否存在别名
                if ((alias.length - 1) == 0) list[sceneSingle[sc]] = [];
                else {
                    var nowalias = (alias[1] && alias[1].length > 0) ? alias[1] : alias[0];
                    list[nowalias] = [alias[0], alias[2]];
                }
            }
        zlist = this.getRoleInfo(list, type);
        return zlist;
    };
    /**
     *  获取当前场景规则详情
     * @param list
     * @param type
     * @returns {Array|*}
     */
    this.getRoleInfo = function (list, type) {
        for (var rule in ruleList) {
            var nailas = false;
            var nvalue = false;
            var key = rule.split("|");
            var value = ruleList[rule].split("|");
            if (list[key[0]] && type) {
                nailas = list[key[0]][0];
                nvalue = list[key[0]][1];
                list[key[0]] = [];
                list[key[0]]['alias'] = nailas ? nailas : key[0];
                list[key[0]]['name'] = nvalue ? nvalue : key[1];
                list[key[0]]['reg'] = value;
            }
            if (!type) {
                list[key[0]] = [];
                list[key[0]]['alias'] = nailas ? nailas : key[0];
                list[key[0]]['name'] = nvalue ? nvalue : key[1];
                list[key[0]]['reg'] = value;
            }
        }
        return list;
    };
    /**
     * 经序列化的二维数组变为一维数组
     * @param data
     * @returns {Array}
     */
    this.getArrayData = function (data) {
        var array = [];
        if (typeof data == 'string') {
            var result = data.split('&');
            expression
            for (var str in result) {
                var string = result[str].split('=');
                array[string[0]] = decodeURI(string[1]);
            }
        } else {
            for (var i in data) {
                if (typeof data[i] == 'object') array[data[i]['name']] = decodeURI(data[i]['value']);
                else {
                    array = data;
                    break;
                }
            }
        }
        return array;
    };
    /**
     *  单个表达式验证
     * @param flag      验证标识
     * @param param     参数
     * @returns {string}
     */
    this.expressionChick = function (flag, allParam) {
        if (arithmetic[flag].indexOf("(*)") > 0){
            var expression = arithmetic[flag].split("(*)");
            for (var i = 1; i <= allParam.length; i++)
                eval("var param" + i + " = '" + allParam[i-1] + "';");
            console.log(
                "param1：", param1?param1:false
                ,"param2：", (typeof param2 !="undefined") ?param2:false
                ,"param3：", (typeof param3 !="undefined") ?param3:false
                ,"param4：", (typeof param4 !="undefined") ?param4:false
                ,"param5：", (typeof param5 !="undefined") ?param5:false
            );
            if (eval(expression[0]))
                return eval(expression[1]);
        }
        return false;
    }
}

regexVerify.prototype.check = function (data, errFunction) {
    //  获取规则列表
    var result = this.getCheckList();
    //  获取数据
    var data = this.getArrayData(data);
    //  重新获取规则
    if (!this.IsScene) result = this.haSubscript(result, data);
    //  验证
    for (var param in data) {
        var regContent = result[param]['reg'];
        var allParam = [data[param]];
        //  查看是否必须检测
        var chickState = true;
        if (regContent.indexOf("require") < 0)
            chickState = this.expressionChick("require", [param])? chickState: false;

        for (var role in regContent) {
            var content = regContent[role].split(":") ? regContent[role].split(":") : regContent[role];
            var flag = regContent[role];
            if (typeof content != "string"){
                flag = content[0];
                for (var info in content)
                    if (info != 0)
                        allParam.push(content[info]);
            }
            var errorMsg = this.expressionChick(flag, allParam);
            if (errorMsg && chickState) {
                if (typeof errFunction == "function")
                    errFunction(result[param]['name'] + errorMsg);
                return errorMsg;
            }
        }
    }
    return false;
};